home *** CD-ROM | disk | FTP | other *** search
/ Alles Voor Internet / Tout Pour Internet / alles voor internet.iso / MacInternet™ / Telnet / NCSA / tn3270 2.4d7 source / NCSA⁄BYU TCP⁄IP / protocol.h < prev    next >
Text File  |  1991-08-06  |  11KB  |  421 lines

  1. /*
  2. *    Protocol structures for network communication
  3. *
  4. ****************************************************************************
  5. *                                                                          *
  6. *      part of:                                                            *
  7. *      TCP/IP kernel for NCSA Telnet                                       *
  8. *      by Tim Krauskopf                                                    *
  9. *                                                                          *
  10. *      National Center for Supercomputing Applications                     *
  11. *      152 Computing Applications Building                                 *
  12. *      605 E. Springfield Ave.                                             *
  13. *      Champaign, IL  61820                                                *
  14. *                                                                          *
  15. *    Copyright (c) 1987, Board of Trustees of the University of Illinois   *
  16. *                                                                          *
  17. ****************************************************************************
  18. *
  19. *  This file contains the structure definitions for each type of 
  20. *  protocol that this program wishes to handle.  A companion file,
  21. *  'protinit.c' initializes sample versions of each type of header, 
  22. *  improving the efficiency of sending packets with constants in most
  23. *  of the fields.
  24. */
  25.  
  26. #include "whatami.h"
  27.  
  28. /************************************************************************/
  29. /*  Ethernet frames
  30. *      All Ethernet transmissions should use this Ethernet header which
  31. *   denotes what type of packet is being sent.
  32. *
  33. *   The header is 14 bytes.  The first 6 bytes are the target's hardware
  34. *   Ethernet address, the second 6 are the sender's hardware address and
  35. *   the last two bytes are the packet type.  Some packet type definitions
  36. *   are included here.
  37. *
  38. *   the two-byte packet type is byte-swapped, PC is lo-hi, Ether is hi-lo
  39. */
  40.  
  41.  
  42. #ifdef PC
  43. #define  EXNS  0x0006           /* probably need swapping */
  44. #define  EIP   0x0008
  45. #define  EARP  0x0608
  46. #define  ERARP    0x3580            /* I guess this is RARP */
  47. #define  ECHAOS  0x0408
  48. #else
  49. #define  EXNS  0x0600           /* these don't need swapping */
  50. #define  EIP   0x0800
  51. #define  EARP  0x0806
  52. #define  ERARP    0x8035
  53. #define  ECHAOS  0x0804
  54. #endif
  55.  
  56. struct ether {
  57.     uint8 
  58.         dest[DADDLEN],                /* where the packet is going */
  59.         me[DADDLEN];                /* who am i to send this packet */
  60.  
  61.     uint16 
  62.         type;                        /* Ethernet packet type  */
  63. };
  64.  
  65. typedef struct ether DLAYER;
  66.  
  67.  
  68. #ifdef notneededanymore
  69.  
  70. #define    EIP        22
  71. #define EARP    23
  72. #define IPSock    72                     /* Later Change to random */
  73.  
  74. struct aTalk {
  75.     uint16 count;
  76.     uint8
  77.          dest[DADDLEN],    /* where the packet is going */
  78.          me[DADDLEN],
  79.         type, paddingchar;        /* Give an even length so we will be at even */
  80.     };
  81.  
  82. typedef struct aTalk DLAYER;
  83.  
  84. #endif
  85.  
  86. /*************************************************************************/
  87. /*  Dave Plummer's  Address Resolution Protocol (ARP) (RFC-826) and 
  88. *   Finlayson, Mann, Mogul and Theimer's Reverse ARP packets.
  89. *
  90. *   Note that the 2 byte ints are byte-swapped.  The protocols calls for
  91. *   in-order bytes, and the PC is lo-hi ordered.
  92. *   
  93. */
  94.  
  95. #define RARPR    0x0004          /*  RARP reply, from host, needs swap */
  96. #define RARPQ    0x0003            /*  RARP request, needs swapping */
  97. #define ARPREP  0x0002          /*  reply, byte swapped when used */
  98. #define ARPREQ  0x0001          /*  request, byte-swapped when used */
  99. #define ARPPRO    0x0800            /*  IP protocol, needs swapping */
  100.  
  101. #define HTYPE     0x0001            /*  Ethernet hardware type, needs swapping */
  102.  
  103. #ifdef noATALKanymore
  104. #define HTYPE    0x0003          /*  Appletalk, will not be swapped */
  105. #endif
  106.  
  107. struct plummer {
  108.     DLAYER d;                 /* data link layer packet header */
  109.  
  110.     uint16
  111.             hrd,            /* hardware type, Ethernet = 1 */
  112.             pro;            /* protocol type to resolve for */
  113.     uint8    
  114.             hln,            /* byte length of hardware addr = 6 for ETNET */
  115.             pln;            /* byte length of protocol = 4 for IP */
  116.     uint16
  117.             op;            /* opcode, request = 1, reply = 2, RARP = 3,4 */
  118.     uint8
  119.             sha[DADDLEN],
  120.             spa[4],
  121.             tha[DADDLEN],
  122.             tpa[4];
  123. /*
  124. *   the final four fields (contained in 'rest') are:
  125. *      sender hardware address:   sha       hln bytes
  126. *      sender protocol address:   spa       pln bytes
  127. *      target hardware address:   tha       hln bytes
  128. *      target protocol address:   tpa       pln bytes
  129. */
  130. };
  131.  
  132. typedef struct plummer ARPKT;
  133.  
  134. /***********************************************************************/
  135. /*  ARP cache
  136. *   Data structure for saving low-level information until needed
  137. */
  138. struct acache {
  139.     uint8
  140.         hrd[DADDLEN],            /* hardware address for this IP address */
  141.         ip[4],                    /* the IP # in question */
  142.         gate;                    /* is this a gateway? */
  143.     int32 
  144.         tm;                        /* time information */
  145. };
  146.  
  147. /***********************************************************************/
  148. /*   Internet protocol
  149. *
  150. */
  151. struct iph {
  152.  
  153.     uint8
  154.         versionandhdrlen;
  155.                             /* I prefer to OR them myself */
  156.                             /* each half is four bits */
  157.     uint8 
  158.         service;            /* type of service for IP */
  159.     uint16
  160.         tlen,                /* total length of IP packet */
  161.         ident,                /* these are all BYTE-SWAPPED! */
  162.         frags;                /* combination of flags and value */
  163.  
  164.     uint8
  165.         ttl,                /* time to live */
  166.         protocol;            /* higher level protocol type */
  167.  
  168.     uint16
  169.         check;                /* header checksum, byte-swapped */
  170.  
  171.     uint8 
  172.         ipsource[4],        /* IP addresses */
  173.         ipdest[4];
  174.  
  175. };
  176.  
  177. typedef struct iph IPLAYER;
  178.  
  179. /*  
  180. *  full IP packet, with data and ip header
  181. */
  182.  
  183. struct ip {
  184.     DLAYER d;
  185.     IPLAYER i;
  186.  
  187.     union {
  188.         uint8 
  189.             data[536];            /* largest recommended, may include options */
  190.         uint8
  191.             options[40];
  192.     } x;
  193. };
  194.  
  195. typedef struct ip IPKT;
  196.  
  197. #define PROTUDP        17
  198. #define PROTTCP        6        /* standard protocol types for IP */
  199. #define PROTICMP    1
  200. /************************************************************************/
  201. /* ICMP packet
  202. *  all of them are of a similar form, some generic fields are spec'd here.
  203. */
  204. struct icmph {
  205.     uint8
  206.         type,                /* ICMP type field */
  207.         code;                /* ICMP code field */
  208.     uint16 
  209.         check,              /* checksum */
  210.         part1,part2;        /* depends on type and code */
  211. };
  212.  
  213. typedef struct icmph ICMPLAYER;
  214.  
  215. struct icmp {
  216.     DLAYER d;
  217.     IPLAYER i;
  218.     ICMPLAYER c;
  219.     uint8 
  220.         data[ICMPMAX];
  221. };
  222.  
  223. typedef struct icmp ICMPKT;
  224.  
  225. /**************************************************************************/
  226. /*  TCP protocol
  227. *      define both headers required and create a data type for a typical
  228. *      outgoing TCP packet (with IP header)
  229. *   
  230. *  Note:  So far, there is no way to handle IP options fields
  231. *    which are associated with a TCP packet.  They are mutually exclusive
  232. *    for both receiving and sending.  Support may be added later.
  233. *
  234. *   The tcph and iph structures can be included in many different types of
  235. *   arbitrary data structures and will be the basis for generic send and
  236. *   receive subroutines later.  For now, the packet structures are optimized 
  237. *   for packets with no options fields.  (seems to be almost all of them from
  238. *   what I've observed.
  239. */
  240.  
  241. struct tcph {
  242.     uint16 
  243.         source,dest;            /* TCP port numbers, all byte-swapped */
  244.     uint32 
  245.         seq,ack;                /* sequence, ACK numbers */
  246.     uint8
  247.         hlen,                        /* length of TCP header in 4 byte words */
  248.         flags;                    /* flag fields */
  249.     uint16
  250.         window,                    /* advertised window, byte-swapped */
  251.         check,                    /* TCP checksum of whole packet */
  252.         urgent;                    /* urgent pointer, when flag is set */
  253. };
  254.  
  255. typedef struct tcph TCPLAYER;
  256.  
  257. /*
  258. *  used for computing checksums in TCP
  259. */
  260. struct pseudotcp {
  261.     uint8 
  262.         source[4],dest[4],        /* IP #'s for source,dest */
  263.         z,proto;                /* zero and protocol number */
  264.     uint16 
  265.         tcplen;                    /* byte-swapped length field */
  266. };
  267.  
  268. struct tcp {
  269.     DLAYER d;
  270.     IPLAYER i;
  271.     TCPLAYER t;
  272.  
  273.     union {
  274.         uint8 
  275.             options[40];        /* not very likely, except on SYN */
  276.         uint8 
  277.             data[TMAXSIZE];    /* largest TCP data we will use */
  278.     } x;
  279. };
  280.  
  281. typedef struct tcp TCPKT;
  282.  
  283. /* 
  284. *  flag field definitions, first two bits undefined
  285. */
  286.  
  287. #define TURG    0x20
  288. #define TACK    0x10
  289. #define TPUSH    0x08
  290. #define TRESET    0x04
  291. #define TSYN    0x02
  292. #define TFIN    0x01
  293.  
  294. /*************************************************************************/
  295. /*   TCP queuing
  296. *   data types for all TCP queuing operations
  297. *   Each open port will have one of these structures assigned to it.
  298. */
  299.  
  300. struct window {
  301.     uint32    
  302.         nxt,                /* sequence number, not byte-swapped */
  303.         ack;                /* what the other machine acked */
  304.     int32
  305.         lasttime;            /* (signed) used for timeout checking */
  306.     uint8
  307.         where[WINDOWSIZE],    /* storage for queue */
  308.         *endbuf,            /* set to end of queue */
  309.         *base,                /* where useful data is in queue */
  310.         *endlim,            /* first spot in queue to add more data */
  311.         push;                /* flag for TCP push */
  312.     uint
  313.         size,                /* size of window advertised */
  314.         port,                /* port numbers from one host or another */
  315.         contain;            /* how many bytes in queue? */
  316. };
  317.  
  318. struct port {
  319.     struct window in,out;
  320.     TCPKT tcpout;                /* pre-initialized as much as possible */
  321.     uint8 state;                /* connection state */
  322.     struct pseudotcp tcps;        /* pseudo-tcp for checksumming */
  323.     int
  324.         credit,                    /* choked-down window for fast hosts */
  325.         sendsize,                /* MTU value for this connection */
  326.         rto;                    /* retrans timeout */
  327. };
  328.  
  329.  
  330. /*************************************************************************/
  331. /*  TCP states
  332. *     each connection has an associated state in the connection flow.
  333. *     the order of the states now matters, those less than a certain
  334. *     number are the "inactive" states.
  335. */
  336. #define SCLOSED 1
  337. #define SLISTEN 2
  338. #define STWAIT    3
  339. #define SSYNR   4
  340. #define SSYNS    5
  341. #define SEST    6
  342. #define SCWAIT    10
  343. #define SFW1    7
  344. #define SFW2    8
  345. #define SCLOSING 9
  346. #define SLAST    11
  347.  
  348. /*
  349. *     services which we will want to use
  350. */
  351. #define HFTP    21
  352. #define HTELNET    23
  353. #define HNAME    42
  354. #define HSUNRPC    111
  355. #define HPRINTER 515
  356.  
  357. /*************************************************************************/
  358. /*  UDP
  359. *   User Datagram Protocol
  360. *   Each packet is an independent datagram, no sequencing information
  361. *
  362. *   UDP uses the identical checksum to TCP
  363. */
  364.  
  365. struct udph {
  366.     uint16
  367.         source,dest;        /* port numbers, all byte-swapped */
  368.     uint16
  369.         length,                /* length of packet, including hdr */
  370.         check;                /* TCP checksum of whole packet */
  371. };
  372.  
  373. typedef struct udph UDPLAYER;
  374.  
  375. struct udp {
  376.     DLAYER d;
  377.     IPLAYER i;
  378.     UDPLAYER u;
  379.     uint8 
  380.         data[UMAXLEN];      /* largest UDP data we will use */
  381. };
  382.  
  383. typedef struct udp UDPKT;
  384.  
  385. struct uport {
  386.     UDPKT udpout;
  387.     struct pseudotcp tcps;        /* pseudo-tcp for checksumming */
  388.     uint16
  389.         listen,                    /* what port should this one listen to? */
  390.         length;                    /* how much data arrived in last packet? */
  391.     uint8 
  392.         data[UMAXLEN],            /* incoming, last datagram of that type */
  393.         who[4],                    /* who sent it to me ? */
  394.         stale;                    /* have we read this packet yet? */
  395. };
  396.  
  397. /*************************************************************************/
  398. /*  event queue
  399. *   records what happens, especially errors, and keeps them for any
  400. *   routines that poll, looking for what happened.
  401. *   Eight event classes are masked in the event class byte.
  402. *    There can be 256 event types per class.
  403. *   The data field is handled differently by each event type.
  404. */
  405. struct eq {
  406.     uint8
  407.         eclass;            /* class, defined in netevent.h */
  408.     int    event;            /* which event */
  409.     int 
  410.         next,            /* ordering for events in queue  */
  411.         idata;            /* integer data, if you feel like it */
  412. };
  413.  
  414. /*  
  415. *  events which can occur and be placed into the event queue
  416. */
  417. #define NEVENTS 50
  418.  
  419. /*  classes defined in netevent.h   */
  420.